<?php

class ParameterValueForm extends CFormModel {
	
	private $row = array();
	private $index = -1;
	
	public function __construct($index, $headers, $values = array())
	{
		/*Constants::print_var(is_array($values)?"array":"not array");
		if (!is_array($values))
		{
			$values = array();
		}*/
		
		$this->index = $index;
		foreach ($headers as $header)
		{
			if (isset($values[$header['mnemonic']]))
			{
				$this->row[$header['mnemonic']] = $values[$header['mnemonic']];
			}else{
				$this->row[$header['mnemonic']] = $this->getDefaultValueForType($header['type']);
			}
		}
	}
	
	private function getDefaultValueForType($type)
	{
		switch($type)
		{
			case 'integer':
			case 'float':
				return 0;
				
			case 'string':
			case 'text':
				return "";
			
			case 'boolean':
				return false;
				
			default:
				return null;
		}
	}
	
	public function attributeNames()
	{
		return array_keys($this->row);
	}
	
	public function __get($name)
	{
		if (array_key_exists($name, $this->row))
		{
			return $this->row[$name];
		}else{
			return parent::__get($name);
		}
	}
	
	public function __set($name, $value)
	{
		if (array_key_exists($name, $this->row))
		{
			$this->row[$name] = $value;
		}else{
			return parent::__set($name, $value);
		}
	}
	
	public function getIndex()
	{
		return $this->index;
	}
}

?>
